package com.example.portlocalforward.controller;

import java.util.List;
import java.util.Optional;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.example.portlocalforward.controller.model.ApiResponse;
import com.example.portlocalforward.controller.model.SshTunnelRequest;
import com.example.portlocalforward.controller.model.SshTunnelResponse;
import com.example.portlocalforward.service.SshTunnelService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;

/**
 * SSH隧道控制器
 * 
 * @author Generated
 * @version 1.0.0
 */
@Api(tags = "SSH隧道管理", description = "SSH隧道管理接口")
@Slf4j
@RestController
@RequestMapping("/api/ssh-tunnel")
public class SshTunnelController {

    @Autowired
    private SshTunnelService sshTunnelService;

    /**
     * 创建SSH隧道
     */
    @ApiOperation(value = "创建SSH隧道", notes = "创建新的SSH隧道配置")
    @PostMapping
    public ResponseEntity<ApiResponse<SshTunnelResponse>> createTunnel(
            @ApiParam(value = "隧道配置信息", required = true) @RequestBody SshTunnelRequest tunnelRequest) {
        try {
            SshTunnelResponse response = sshTunnelService.createTunnel(tunnelRequest);
            return ResponseEntity.ok(ApiResponse.success("隧道创建成功", response));
        } catch (RuntimeException e) {
            log.error("创建隧道失败", e);
            return ResponseEntity.ok(ApiResponse.error(e.getMessage()));
        } catch (Exception e) {
            log.error("创建隧道失败", e);
            return ResponseEntity.ok(ApiResponse.error("创建隧道失败: " + e.getMessage()));
        }
    }

    /**
     * 根据ID获取隧道
     */
    @ApiOperation(value = "获取隧道详情", notes = "根据ID获取隧道详细信息")
    @GetMapping("/{id}")
    public ResponseEntity<ApiResponse<SshTunnelResponse>> getTunnel(
            @ApiParam(value = "隧道ID", required = true) @PathVariable Long id) {
        try {
            Optional<SshTunnelResponse> tunnelOpt = sshTunnelService.getTunnelById(id);
            if (!tunnelOpt.isPresent()) {
                return ResponseEntity.ok(ApiResponse.error("隧道不存在"));
            }
            return ResponseEntity.ok(ApiResponse.success(tunnelOpt.get()));
        } catch (Exception e) {
            log.error("获取隧道失败", e);
            return ResponseEntity.ok(ApiResponse.error("获取隧道失败: " + e.getMessage()));
        }
    }

    /**
     * 获取所有隧道
     */
    @ApiOperation(value = "获取所有隧道", notes = "获取所有隧道列表")
    @GetMapping
    public ResponseEntity<ApiResponse<List<SshTunnelResponse>>> getAllTunnels() {
        try {
            List<SshTunnelResponse> tunnels = sshTunnelService.getAllTunnels();
            return ResponseEntity.ok(ApiResponse.success(tunnels));
        } catch (Exception e) { 
            log.error("获取隧道列表失败", e);
            return ResponseEntity.ok(ApiResponse.error("获取隧道列表失败: " + e.getMessage()));
        }
    }

    /**
     * 更新隧道
     */
    @ApiOperation(value = "更新隧道", notes = "更新隧道配置信息")
    @PutMapping("/{id}")
    public ResponseEntity<ApiResponse<SshTunnelResponse>> updateTunnel(
            @ApiParam(value = "隧道ID", required = true) @PathVariable Long id,
            @ApiParam(value = "隧道配置信息", required = true) @RequestBody SshTunnelRequest tunnelRequest) {
        try {
            SshTunnelResponse response = sshTunnelService.updateTunnel(id, tunnelRequest);
            return ResponseEntity.ok(ApiResponse.success("隧道更新成功", response));
        } catch (RuntimeException e) {
            log.error("更新隧道失败", e);
            return ResponseEntity.ok(ApiResponse.error(e.getMessage()));
        } catch (Exception e) {
            log.error("更新隧道失败", e);
            return ResponseEntity.ok(ApiResponse.error("更新隧道失败: " + e.getMessage()));
        }
    }

    /**
     * 删除隧道
     */
    @ApiOperation(value = "删除隧道", notes = "根据ID删除隧道")
    @DeleteMapping("/{id}")
    public ResponseEntity<ApiResponse<Void>> deleteTunnel(
            @ApiParam(value = "隧道ID", required = true) @PathVariable Long id) {
        try {
            sshTunnelService.deleteTunnel(id);
            return ResponseEntity.ok(ApiResponse.success("隧道删除成功", null));
        } catch (RuntimeException e) {
            log.error("删除隧道失败", e);
            return ResponseEntity.ok(ApiResponse.error(e.getMessage()));
        } catch (Exception e) {
            log.error("删除隧道失败", e);
            return ResponseEntity.ok(ApiResponse.error("删除隧道失败: " + e.getMessage()));
        }
    }

    /**
     * 根据名称搜索隧道
     */
    @ApiOperation(value = "搜索隧道", notes = "根据隧道名称模糊搜索")
    @GetMapping("/search")
    public ResponseEntity<ApiResponse<List<SshTunnelResponse>>> searchTunnels(
            @ApiParam(value = "隧道名称关键字") @RequestParam String name) {
        try {
            List<SshTunnelResponse> tunnels = sshTunnelService.findTunnelsByName(name);
            return ResponseEntity.ok(ApiResponse.success(tunnels));
        } catch (Exception e) {
            log.error("搜索隧道失败", e);
            return ResponseEntity.ok(ApiResponse.error("搜索隧道失败: " + e.getMessage()));
        }
    }
}
